package org.wanda.pv.controller.basic;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.lang.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.wanda.pv.framework.base.Result;
import org.wanda.pv.framework.page.Pagination;
import org.wanda.pv.model.basic.Point;
import org.wanda.pv.model.basic.TopPoint;
import org.wanda.pv.service.basic.TopPointService;

import java.util.List;

@RestController
@RequestMapping("/topPoint")
public class TopPointController {

    private final static Logger log = LoggerFactory.getLogger(TopPointController.class);


    @Autowired
    private Dao dao;
    @Autowired
    private TopPointService topPointService;

    @RequestMapping("/getTopPointBySys")
    public Object getTopPointBySys(Long sysId){
        List<TopPoint> topPoints = topPointService.query(Cnd.where("systemId", "=", sysId));
        topPoints.forEach(item->dao.fetchLinks(item,null));

        return Result.success("success",topPoints);


    }

    @RequestMapping("/list")
    public Object list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       String name){
        Cnd cnd = Cnd.where(null);

        if (Strings.isNotBlank(name))
             cnd = cnd.and("name", "like", "%" + name + "%");
        Pagination listPage = topPointService.listPage(pageNum, pageSize, cnd);
        listPage.getList().forEach(item->dao.fetchLinks(item,null));


        return Result.success("success",listPage);
    }

    @RequestMapping("/add")
    public Object add(String name,
                      String number,
                      Long specId,
                      Long sysId) {
        try {
            TopPoint topPoint = new TopPoint();
            topPoint.setName(name);
            topPoint.setNumber(number);
            topPoint.setSpecialtyId(specId);
            topPoint.setSystemId(sysId);
            dao.insert(topPoint);

            return Result.success("success", "操作成功!");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("fail", "服务器内部错误!");

        }

    }


    @RequestMapping("/update")
    public Object update(
            Long id,
            String name,
            String number,
            Long specId,
            Long sysId) {

        try{
            TopPoint topPoint = dao.fetch(TopPoint.class,id);

            topPoint.setName(name);
            topPoint.setNumber(number);
            topPoint.setSpecialtyId(specId);
            topPoint.setSystemId(sysId);
            topPointService.updateIgnoreNull(topPoint);
            return Result.success("success", "操作成功!");
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return Result.error("fail", "服务器内部错误!");
        }

    }

    @RequestMapping("/del")
    public Object del(Long id){

        int count = dao.count(Point.class, Cnd.where("topPointId", "=", id));
        if (count>0){
            return  Result.error("fail","存在子项,禁止删除!");
        }

        try {
            dao.delete(TopPoint.class,id);
            return Result.success("success", "操作成功!");
        }catch (Exception e ){
            log.error(e.getMessage(), e);
            return Result.error("fail", "服务器内部错误!");
        }


    }


}
